package io.aircraft.modules.wms.controller;

import io.aircraft.common.Utils.ExcelUtils;
import io.aircraft.common.annotation.LogOperation;
import io.aircraft.common.constant.Constant;
import io.aircraft.common.page.PageData;
import io.aircraft.common.utils.Result;
import io.aircraft.common.validator.AssertUtils;
import io.aircraft.common.validator.ValidatorUtils;
import io.aircraft.common.validator.group.AddGroup;
import io.aircraft.common.validator.group.DefaultGroup;
import io.aircraft.common.validator.group.UpdateGroup;
import io.aircraft.modules.wms.dto.ContainerDTO;
import io.aircraft.modules.wms.excel.ContainerExcel;
import io.aircraft.modules.wms.service.ContainerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 * 托盘信息
 *
 * @author Cycle gengruyi01@163.com
 * @since 1.0.0 2022-12-12
 */
@RestController
@RequestMapping("wms/container")
@Api(tags="托盘信息")
public class ContainerController {
    @Autowired
    private ContainerService containerService;

    @GetMapping("page")
    @ApiOperation("分页")
    @RequiresPermissions("wms:container:page")
    public Result<PageData<ContainerDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<ContainerDTO> page = containerService.page(params);

        return new Result<PageData<ContainerDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    @RequiresPermissions("wms:container:info")
    public Result<ContainerDTO> get(@PathVariable("id") Long id){
        ContainerDTO data = containerService.get(id);

        return new Result<ContainerDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    @RequiresPermissions("wms:container:save")
    public Result save(@RequestBody ContainerDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        containerService.save(dto);

        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    @RequiresPermissions("wms:container:update")
    public Result update(@RequestBody ContainerDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        containerService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    @RequiresPermissions("wms:container:delete")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        containerService.delete(ids);

        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    @RequiresPermissions("wms:container:export")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<ContainerDTO> list = containerService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, list, ContainerExcel.class);
    }

}